﻿//main
var fetch = false;
var nPage = 0;
var pageSize;

$(document).ready(function () {
    try {
        $('#loadScroll').hide();
        fetch = true;
        getData();
    }
    catch (err) {
        alert("Error description: " + err.message + "\n\n");
    }

    $(window).scroll(function () {
        if ($(window).scrollTop() == $(document).height() - $(window).height()) {
            if (fetch == true) {
                getData();
            }
        }
    });
});

function getData() {

    //if ($("#crawler_container").height() < $(document).height())
    //    pageSize = 25;
    //else
    //    pageSize = 20;

    pageSize = 10;

    $.ajax({
        url: '/Home/GetFeed',
        data: { "pageIndex": nPage, "pageSize": pageSize, "provider": $("#hidprovider").val(), "keywords": $("#hKeywords").val() },
        beforeSend: function (xhr) {
            if (nPage != 0) {
                var toTalHeight = 0;
                $('.list').each(function () {
                    toTalHeight += $(this).height();
                });
                $('#crawler_container').css('height', toTalHeight + (nPage+1)*52);
            }
            $('#loadScroll').show();
        },
        success: function (response) {
            showFeeds(response);
        },
        complete: function (xhr) {
            $('#loadScroll').hide();
        },
        error: function (xhr, textStatus, errorThrown) {
            alert("ERROR");
            return;
        }
    });
}

function showFeeds(response) {
    try {
        var data = response;
        fetch = data.canFetch;
        nPage = data.nextPage;
        var html = '';
        if (data.crawlers != null && data.crawlers.length > 0) {
            $.each(data.crawlers, function (i, item) {
                html += '<div class="list">';
                if (item.DisplayUrl)
                    html += '<a class="image" href="' + item.ClickUrl + '" target="_blank"><img  width="100%" src=' + item.DisplayUrl + ' /></a>';
                html += '<div class="textDetail">';
                html += '<a class="title" href="'+item.ClickUrl+'" target="_blank">'+item.Title+'</a><br>';
                html += '<a class="description" href="' + item.ClickUrl + '" target="_blank">' + item.Description + '</a>';
                if (item.UpdateDate)
                    html += '<div>Cập nhật: ' + item.UpdateDate + '</div></div></div>';
                else
                    html += '</div></div>';
            });
            $('#crawler_list').append(html);
        }
        else {
            $('#crawler_list').append('<div>Không tìm thấy kết quả nào. Vui lòng thử các kết quả liên quan sau:</div>');
            $.ajax({
                url: '/Home/GetTopKeywords',
                data: { "keywords": $("#hKeywords").val() },
                success: function (response) {
                    if (response.topKeywords != null && response.topKeywords.length > 0)
                    {
                        $.each(response.topKeywords, function (i, item) {
                            html += '<div class="list">';
                            html += '<div class="textDetail">';
                            html += '<a class="title" href="' + item.ClickUrl + '" target="_blank">' + item.Title + '</a><br></div></div>';
                        });
                        $('#crawler_list').append(html);
                    }
                    
                },
                complete: function (xhr) {
                    $('#loadScroll').hide();
                },
                error: function (xhr, textStatus, errorThrown) {
                    alert("ERROR");
                    return;
                }
            });
        }
    }
    catch (err) {
        alert("Error description: " + err.message + "\n\n");
    }
}
